package UI;

import java.awt.Font;
import java.awt.Rectangle;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.JOptionPane;

import oocis.user.*;

public class RegisterUI extends JFrame {

	private static final long serialVersionUID = 1L;

	private JPanel jContentPane = null;

	private JLabel jLabel = null;

	private JLabel jLabel1 = null;

	private JLabel jLabel2 = null;

	private JLabel jLabel3 = null;

	private JLabel jLabel4 = null;

	private JTextField jTextField = null;

	private JButton jButton = null;

	private JPasswordField jPasswordField = null;

	private JPasswordField jPasswordField1 = null;

	private JButton jButton1 = null;

	private JButton jButton2 = null;

	/**
	 * This is the default constructor
	 */
	public RegisterUI() {
		super();
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(360, 270);
		this.setContentPane(getJContentPane());
		this.setTitle("Register");
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jLabel4 = new JLabel();
			jLabel4.setBounds(new Rectangle(15, 160, 139, 20));
			jLabel4.setText("Retype the password :");
			jLabel3 = new JLabel();
			jLabel3.setBounds(new Rectangle(15, 130, 137, 20));
			jLabel3.setText("Password : ");
			jLabel2 = new JLabel();
			jLabel2.setBounds(new Rectangle(15, 100, 136, 20));
			jLabel2.setText("User name :");
			jLabel1 = new JLabel();
			jLabel1.setBounds(new Rectangle(15, 70, 238, 20));
			jLabel1.setText("Input the following informations");
			jLabel = new JLabel();
			jLabel.setBounds(new Rectangle(15, 20, 176, 30));
			jLabel.setFont(new Font("Arial", Font.BOLD, 18));
			jLabel.setText("OOCIS Register");
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.add(jLabel, null);
			jContentPane.add(jLabel1, null);
			jContentPane.add(jLabel2, null);
			jContentPane.add(jLabel3, null);
			jContentPane.add(jLabel4, null);
			jContentPane.add(getJTextField(), null);
			jContentPane.add(getJButton(), null);
			jContentPane.add(getJPasswordField(), null);
			jContentPane.add(getJPasswordField1(), null);
			jContentPane.add(getJButton1(), null);
			jContentPane.add(getJButton2(), null);
		}
		return jContentPane;
	}

	/**
	 * This method initializes jTextField
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getJTextField() {
		if (jTextField == null) {
			jTextField = new JTextField();
			jTextField.setBounds(new Rectangle(165, 100, 107, 20));
		}
		return jTextField;
	}

	/**
	 * This method initializes jButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getJButton() {
		if (jButton == null) {
			jButton = new JButton();
			jButton.setBounds(new Rectangle(275, 100, 70, 20));
			jButton.setText("Check");
			jButton.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					String _username = jTextField.getText();
					userManage um = new userManage();
					boolean isExist = um.checkname(_username);
					if (isExist) {
						JOptionPane
								.showMessageDialog(
										null,
										"The username is not available, continue please.",
										"Checking",
										JOptionPane.INFORMATION_MESSAGE);
					} else {
						JOptionPane
								.showMessageDialog(
										null,
										"The username is available, continue registering please.",
										"Checking",
										JOptionPane.INFORMATION_MESSAGE);
					}
				}
			});
		}
		return jButton;
	}

	/**
	 * This method initializes jPasswordField
	 * 
	 * @return javax.swing.JPasswordField
	 */
	private JPasswordField getJPasswordField() {
		if (jPasswordField == null) {
			jPasswordField = new JPasswordField();
			jPasswordField.setBounds(new Rectangle(165, 130, 181, 20));
		}
		return jPasswordField;
	}

	/**
	 * This method initializes jPasswordField1
	 * 
	 * @return javax.swing.JPasswordField
	 */
	private JPasswordField getJPasswordField1() {
		if (jPasswordField1 == null) {
			jPasswordField1 = new JPasswordField();
			jPasswordField1.setBounds(new Rectangle(165, 160, 182, 20));
		}
		return jPasswordField1;
	}

	/**
	 * This method initializes jButton1
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getJButton1() {
		if (jButton1 == null) {
			jButton1 = new JButton();
			jButton1.setBounds(new Rectangle(60, 200, 100, 30));
			jButton1.setText("Register");
			jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					String _username = jTextField.getText();
					String _password = jPasswordField.getText();
					String _repassword = jPasswordField1.getText();
					int a = JOptionPane.showConfirmDialog(null,
							"Are you sure?", "Register",
							JOptionPane.YES_NO_OPTION);
					if (a == 0) {
						userManage um = new userManage();
						boolean isExist = um.checkname(_username);
						if (isExist) {
							JOptionPane
									.showMessageDialog(null,
											"The username is not available",
											"Checking",
											JOptionPane.INFORMATION_MESSAGE);
						} else if (_password.equals(_repassword)) {
							um.createUser(_username, _password);
							JOptionPane
									.showMessageDialog(
											null,
											"User "
													+ _username
													+ " has been created.\n Please close this window and sign in.",
											"Success",
											JOptionPane.INFORMATION_MESSAGE);

						} else {
							JOptionPane.showMessageDialog(null,
									"The password not match!", "Error",
									JOptionPane.INFORMATION_MESSAGE);
						}
					}

				}
			});
		}
		return jButton1;
	}

	public void closeit() {
		this.dispose();
	}

	/**
	 * This method initializes jButton2
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getJButton2() {
		if (jButton2 == null) {
			jButton2 = new JButton();
			jButton2.setBounds(new Rectangle(210, 200, 100, 30));
			jButton2.setText("Cancel");
			jButton2.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {

					closeit();
					// Auto-generated
					// Event stub
					// mouseClicked()
				}
			});
		}
		return jButton2;
	}

} // @jve:decl-index=0:visual-constraint="187,24"
